package com.zzyl.controller.customer;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ReservationDto;
import com.zzyl.dto.ReservationQueryDto;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
@Slf4j
public class CustomerReservationController {
    @Autowired
    private ReservationService reservationService;

    /**
     * 查询取消预约数量
     *
     * @return {@code ResponseResult<Integer> }
     * @author YiSheep
     */
    @ApiOperation("查询取消预约数量")
    @GetMapping("/cancelled-count")
    public ResponseResult<Integer> cancelledCount() {
        log.info("查询取消预约数量");
        Integer count = reservationService.getCancelledCount();
        return ResponseResult.success(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time 预约日期
     * @return {@code ResponseResult<TimeCountVo> }
     * @author YiSheep
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("countByTime")
    public ResponseResult<List<TimeCountVo>> countByTime(Long time) {
        log.info("查询每个时间段剩余预约次数:{}", time);
        List<TimeCountVo> timeCountVos = reservationService.countByTimeRange(time);
        return ResponseResult.success(timeCountVos);
    }

    /**
     * 新增预约
     *
     * @param dto 预约Dto
     * @return {@code ResponseResult<String> }
     * @author YiSheep
     */
    @ApiOperation("新增预约")
    @PostMapping
    public ResponseResult<Void> addReservation(@RequestBody ReservationDto dto) {
        log.info("新增预约:{}", dto);
        reservationService.addReservation(dto);
        return ResponseResult.success();
    }

    /**
     * 分页查询预约
     *
     * @param dto 预约分页条件查询Dto
     * @return {@code ResponseResult<PageBean<ReservationVo>> }
     * @author YiSheep
     */
    @ApiOperation("分页查询预约")
    @GetMapping("/page")
    public ResponseResult<PageBean<ReservationVo>> findByPage(ReservationQueryDto dto) {
        log.info("分页查询预约:{}", dto);
        PageBean<ReservationVo> pageBean = reservationService.findByPage(dto);
        return ResponseResult.success(pageBean);
    }

    /**
     * 取消预约
     *
     * @param id 预约ID
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public ResponseResult<Void> cancelReservation(@PathVariable Long id) {
        log.info("取消预约:{}", id);
        reservationService.cancelReservation(id);
        return ResponseResult.success();
    }
}
